#include "ChademoSendProcess.h"
#include "usart.h"
#include "ChargeCtrl.h"
#include "uuPowerProcess.h"
#include "ccsLogicProcess.h"

//ReportInit
#define ReportInitReq_ID				0xF1
#define ReportInitReq_Len				4
//Start
#define StartReq_ID						0xFC
#define StartReq_Len					4
//AllStop
#define AllStopReq_ID					0xFE
#define AllStopReq_Len					4
//ReportState
#define ReportStateReq_ID				0xF5
#define ReportStateReq_Len				4
//SessionSetup
#define CHADEMO_SessionSetupRes_ID				0x82
#define CHADEMO_SessionSetupRes_Len				4
//ChargeParameterDiscovery
#define CHADEMO_ChargeParameterDiscoveryRes_ID	0x88
#define CHADEMO_ChargeParameterDiscoveryRes_Len	16
//CableCheck
#define CHADEMO_CableCheckRes_ID				0x8F
#define CHADEMO_CableCheckRes_Len				20
//PowerDelivery
#define CHADEMO_PowerDeliveryRes_ID				0x89
#define CHADEMO_PowerDeliveryRes_Len			16
//CurrentDemand
#define CHADEMO_CurrentDemandRes_ID				0x91
#define CHADEMO_CurrentDemandRes_Len			28
//WeldingDetection
#define CHADEMO_WeldingDetectionRes_ID			0x92
#define CHADEMO_WeldingDetectionRes_Len			12
//SessionStop
#define CHADEMO_SessionStopRes_ID				0x8C
#define CHADEMO_SessionStopRes_Len				4

void CHAdeMO_ReportInitReq(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t reqMsgBuf[12];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	reqMsgBuf[idx++] = 'R';
	reqMsgBuf[idx++] = 'N';
	reqMsgBuf[idx++] = ReportInitReq_ID;	//MsgID：0xF1
	reqMsgBuf[idx++] = ReportInitReq_Len;	//MsgLen:4
	reqMsgBuf[idx++] = 0x01;//0:Initial CP voltage 12V, 1:Initial CP voltage -12V. It is for avoiding the problem with VW’s EV 
	reqMsgBuf[idx++] = 0;	//0:No CP State F，	1~20:CP State F retain time(sec), 0xA5: Plugin and Start without startReq
	reqMsgBuf[idx++] = 0;	//SLAC wait time 0:default(7sec), 可选1~60 seconds
	reqMsgBuf[idx++] = 0;	//SLAC re-start counter.0:default(TUV=3sec, Field=8s)
	for(i=0;i<ReportInitReq_Len;i++)
	{
		chksum += reqMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		reqMsgBuf[idx++] = chksumBuf[i];
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, reqMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, reqMsgBuf, idx);
		break;
		default:
			break;
	}
	rnlChademoCommCtrl[gunId].sendFlag.flagBit.rptInit = 1;
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("ReportInitReq: ");
	for(i=0; i<ReportInitReq_Len + 8; i++)
	{
		printf("%02X ",reqMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_StartReq(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t reqMsgBuf[15];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	reqMsgBuf[idx++] = 'R';
	reqMsgBuf[idx++] = 'N';
	reqMsgBuf[idx++] = StartReq_ID;	//MsgID:0xFC
	reqMsgBuf[idx++] = StartReq_Len;//MsgLen:4
	
	reqMsgBuf[idx++] = para.chargeType;//Info[0] = 0:CCS_Mode, 1:CHAdeMO_Mode, 2:AC_Mode(不用)
	reqMsgBuf[idx++] = 0;			//CHAdeMO Only:Info[1] == 0: Basic,1: Advanced(standard compliance test),2: Custom
	reqMsgBuf[idx++] = 0;	
	reqMsgBuf[idx++] = 0;	
	for(i=0;i<StartReq_Len;i++)
	{
		chksum += reqMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)					
	{
		reqMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, reqMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, reqMsgBuf, idx);
		break;
		default:
			break;
	}
	rnlChademoCommCtrl[gunId].sendFlag.flagBit.start = 1;

	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("StartChargeReq: ");
	for(i=0; i<StartReq_Len + 8; i++)
	{
		printf("%02X ",reqMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_AllStopReq(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t reqMsgBuf[15];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];

	uint8_t i;
	reqMsgBuf[idx++] = 'R';
	reqMsgBuf[idx++] = 'N';
	reqMsgBuf[idx++] = AllStopReq_ID;	//MsgID:0xFE
	reqMsgBuf[idx++] = AllStopReq_Len;	//MsgLen:4
	/*	[0] : C -> CHAdeMO all stop request
		[1]: EVSEStatusCode
		dinDC_EVSEStatusCodeType_EVSE_Malfunction = 5
		dinDC_EVSEStatusCodeType_EVSE_EmergencyShutdown = 6
		dinDC_EVSEStatusCodeType_EVSE_BatteryIncompatibility = 12
		dinDC_EVSEStatusCodeType_EVSE_ChargingSystemMalfunction = 13
	*/
	reqMsgBuf[idx++] = 'C';
	reqMsgBuf[idx++] = para.evseStatus.StatusCode;
	reqMsgBuf[idx++] = 0;
	reqMsgBuf[idx++] = 0;
	for(i=0;i<AllStopReq_Len;i++)
	{
		chksum += reqMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		reqMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, reqMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, reqMsgBuf, idx);
		break;
		default:
			break;
	}
	rnlChademoCommCtrl[gunId].sendFlag.flagBit.aStop = 1;
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("AllStopReq: ");
	for(i=0; i<AllStopReq_Len + 8; i++)
	{
		printf("%02X ",reqMsgBuf[i]);
	}
	printf("\n");
	#endif
}

// 暂时用不到
void CHAdeMO_ReportStateReq(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t reqMsgBuf[15];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	reqMsgBuf[idx++] = 'R';
	reqMsgBuf[idx++] = 'N';
	reqMsgBuf[idx++] = ReportStateReq_ID;	//MsgID:0xf5
	reqMsgBuf[idx++] = ReportStateReq_Len;	//MsgLen:4
	reqMsgBuf[idx++] = para.reportStateType;//TYPE	 
	reqMsgBuf[idx++] = 0;					//Info[3]
	reqMsgBuf[idx++] = 0;	
	reqMsgBuf[idx++] = 0;	
	for(i=0;i<ReportStateReq_Len;i++)
	{
		chksum += reqMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		reqMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, reqMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, reqMsgBuf, idx);
		break;
		default:
			break;
	}
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("ReportStateReq: ");
	for(i=0; i<ReportStateReq_Len + 8; i++)
	{
		printf("%02X ",reqMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_SessionSetupRes(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t resMsgBuf[20];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_SessionSetupRes_ID;	//MsgID:0x82
	resMsgBuf[idx++] = CHADEMO_SessionSetupRes_Len;	//MsgLen:4
	//ResponseCode
	resMsgBuf[idx++] = para.responseCode;
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//checkSum
	for(i=0;i<CHADEMO_SessionSetupRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}
	rnlChademoCommCtrl[gunId].sendFlag.flagBit.sSetup = 1;
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("SessionSetupRes: ");
	for(i=0; i<CHADEMO_SessionSetupRes_Len + 8; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_ChargeParameterDiscoveryRes(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t resMsgBuf[30];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_ChargeParameterDiscoveryRes_ID;	//MsgID:0x88
	resMsgBuf[idx++] = CHADEMO_ChargeParameterDiscoveryRes_Len;	//MsgLen:16
	resMsgBuf[idx++] = para.responseCode;					//ResponseCode
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//EVSEStatusCode
	resMsgBuf[idx++] = para.evseStatus.StatusCode;
	//Padding[3]
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;	
	//EVSEMaximumCurrentLimit 
	resMsgBuf[idx++] = 0xff;    //0.1A
	resMsgBuf[idx++] = unit_A;
	resMsgBuf[idx++] = (uint8_t)(para.evseMaxCur);			//todo wait     set max current
	resMsgBuf[idx++] = (uint8_t)((para.evseMaxCur) >> 8);	//todo wait     set max current
	//EVSEMaximumVoltageLimit
	resMsgBuf[idx++] = 0xff;    //0.1V
	resMsgBuf[idx++] = unit_V;
	resMsgBuf[idx++] =(uint8_t)(para.evseMaxVol);         //todo wait  equal car  max  voltage
    resMsgBuf[idx++] =(uint8_t)((para.evseMaxVol) >> 8);  //todo wait equal car  max  voltage

	//check sum 
	for(i=0;i<CHADEMO_ChargeParameterDiscoveryRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i];
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("ChargeParameterDiscoveryRes: ");
	for(i=0; i<CHADEMO_ChargeParameterDiscoveryRes_Len + 8; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
	
}


void CHAdeMO_PowerDeliveryRes(RNL_CHAdeMOEvsePara *para, uint8_t gunId)
{
	uint8_t resMsgBuf[30];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_PowerDeliveryRes_ID;	//MsgID:0x89
	resMsgBuf[idx++] = CHADEMO_PowerDeliveryRes_Len;//MsgLen:16
	resMsgBuf[idx++] = para->responseCode;		
	//Padding[3]
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//EVSEStatusCode
	resMsgBuf[idx++] = para->evseStatus.StatusCode;
	//Padding
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;

	para->evsePresentVol = uuGetModuleVout(gunId);
	para->evsePresentCur = uuGetModuleIout(gunId);

	//EVSEPresentVoltage
	resMsgBuf[idx++] = 0xff;	//分辨率0.1
	resMsgBuf[idx++] = unit_V;	//单位为V
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentVol)&0xFF);
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentVol)>>8)&0xFF);
	//EVSEPresentCurrent
	resMsgBuf[idx++] = 0xff;	//分辨率0.1
	resMsgBuf[idx++] = unit_A;	//单位为A
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentCur)&0xFF);
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentCur)>>8)&0xFF);
	//checkSum
	for(i=0;i<CHADEMO_PowerDeliveryRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("PowerDeliveryRes: ");
	for(i=0; i<CHADEMO_PowerDeliveryRes_Len + 8; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_CableCheckRes(RNL_CHAdeMOEvsePara *para, uint8_t gunId)
{
	uint8_t resMsgBuf[30];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_CableCheckRes_ID;	//MsgID:0x8F
	resMsgBuf[idx++] = CHADEMO_CableCheckRes_Len;	//MsgLen:20
	resMsgBuf[idx++] = para->responseCode;			//ResponseCode
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//EVSE  process : 1(Ongoing) or 0(Finished)
	resMsgBuf[idx++] = para->evseProcess; 	
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//EVSEStatus.EVSEStatusCode
	resMsgBuf[idx++] = para->evseStatus.StatusCode;	
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;

	para->evsePresentVol = uuGetModuleVout(gunId);
	para->evsePresentCur = uuGetModuleIout(gunId);


	//EVSEPresentVoltage
	resMsgBuf[idx++] = 0xff;	//分辨率0.1
	resMsgBuf[idx++] = unit_V;	//单位为V
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentVol)&0xFF);
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentVol)>>8)&0xFF);
	//EVSEPresentCurrent
	resMsgBuf[idx++] = 0xff;	//分辨率0.1
	resMsgBuf[idx++] = unit_A;	//单位为A
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentCur)&0xFF);
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentCur)>>8)&0xFF);
	
	for(i=0;i<CHADEMO_CableCheckRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("CableCheckRes: ");
	for(i=0; i<CHADEMO_CableCheckRes_Len + 8; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_CurrendDemandRes(RNL_CHAdeMOEvsePara *para, uint8_t gunId)
{
	uint8_t resMsgBuf[45];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_CurrentDemandRes_ID;	//MsgID:0x91
	resMsgBuf[idx++] = CHADEMO_CurrentDemandRes_Len;//MsgLen:28
	resMsgBuf[idx++] = para->responseCode;			//ResponseCode
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//EVSEStatus.EVSEStatusCode
	resMsgBuf[idx++] = para->evseStatus.StatusCode;	
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	

	para->evsePresentCur = uuGetModuleIout(gunId);
	para->evsePresentVol = uuGetModuleVout(gunId);


	//EVSEPresentVoltage
	resMsgBuf[idx++] = 0xff;	//分辨率0.1V
	resMsgBuf[idx++] = unit_V;	//单位为V
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentVol)&0XFF);//先低位后高位
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentVol)>>8)&0XFF);
	//EVSEPresentCurrent
	resMsgBuf[idx++] = 0xff;	//分辨率0.1A
	resMsgBuf[idx++] = unit_A;	//单位为A
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentCur)&0XFF);//先低位后高位
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentCur)>>8)&0XFF);
	//EVSECurrentLimitAchieved
	resMsgBuf[idx++] = para->currentLimitAchieved;
	//EVSEVoltageLimitAchieved
	resMsgBuf[idx++] = para->voltageLimitAchieved;
	//Padding[2] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	//EVSEMaximumVoltageLimit 
	resMsgBuf[idx++] = 0xff;	//分辨率0.1V
	resMsgBuf[idx++] = unit_V;	//单位为V
	resMsgBuf[idx++] = (uint8_t)((para->evseMaxVol)&0XFF);			//todo wait  equal car  max  voltage
	resMsgBuf[idx++] = (uint8_t)(((para->evseMaxVol)>>8)&0XFF);		//todo wait  equal car  max  voltage
	//EVSEMaximumCurrentLimit
	resMsgBuf[idx++] = 0xff;	//分辨率0.1A
	resMsgBuf[idx++] = unit_A;	//单位为A
	resMsgBuf[idx++] = (uint8_t)((para->evseMaxCur)&0xFF);//先低位后高位
	resMsgBuf[idx++] = (uint8_t)(((para->evseMaxCur)>>8)&0xFF);

	for(i=0;i<CHADEMO_CurrentDemandRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}

	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("CurrendDemandRes: ");
	for(i = 0; i < idx; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
}



void CHAdeMO_WeldingDetectionRes(RNL_CHAdeMOEvsePara *para, uint8_t gunId)
{
	uint8_t resMsgBuf[25];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_WeldingDetectionRes_ID;	//MsgID:0x92
	resMsgBuf[idx++] = CHADEMO_WeldingDetectionRes_Len;	//MsgLen:12
	resMsgBuf[idx++] = para->responseCode;				//ResponseCode
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;

	para->evsePresentVol = uuGetModuleVout(gunId);
	para->evsePresentCur = uuGetModuleIout(gunId);

	//EVSEPresentVoltage
	resMsgBuf[idx++] = 0xff;	// 分辨率0.1V
	resMsgBuf[idx++] = unit_V;	// 单位为V
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentVol)&0xFF);// 先低位后高位
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentVol)>>8)&0xFF);
	//EVSEPresentCurrent
	resMsgBuf[idx++] = 0xff;	// 分辨率0.1A
	resMsgBuf[idx++] = unit_A;	// 单位为A
	resMsgBuf[idx++] = (uint8_t)((para->evsePresentCur)&0xFF);// 先低位后高位
	resMsgBuf[idx++] = (uint8_t)(((para->evsePresentCur)>>8)&0xFF);
	for(i=0;i<CHADEMO_WeldingDetectionRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("WeldingDetectionRes: ");
	for(i = 0; i < idx; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
}

void CHAdeMO_SessionStopRes(RNL_CHAdeMOEvsePara para, uint8_t gunId)
{
	uint8_t resMsgBuf[15];
	uint32_t idx = 0;
	uint32_t chksum = 0;
	uint8_t chksumBuf[4];
	uint8_t i;
	resMsgBuf[idx++] = 'R';
	resMsgBuf[idx++] = 'N';
	resMsgBuf[idx++] = CHADEMO_SessionStopRes_ID;	//MsgID:0x8C
	resMsgBuf[idx++] = CHADEMO_SessionStopRes_Len;	//MsgLen:4
	resMsgBuf[idx++] = para.responseCode;		//ResponseCode
	//Padding[3] = {0}
	resMsgBuf[idx++] = 0;
	resMsgBuf[idx++] = 0;	
	resMsgBuf[idx++] = 0;
	
	for(i=0;i<CHADEMO_SessionStopRes_Len;i++)
	{
		chksum += resMsgBuf[4 + i]; 
	}
	chksumBuf[0] = (uint8_t)(chksum & 0xFF);
	chksumBuf[1] = (uint8_t)((chksum >> 8) & 0xFF);
	chksumBuf[2] = (uint8_t)((chksum >> 16) & 0xFF);
	chksumBuf[3] = (uint8_t)((chksum >> 24) & 0xFF);
	for(i=0;i<4;i++)
	{
		resMsgBuf[idx++] = chksumBuf[i];//Checksum
	}
	switch(gunId)
	{
		case GUNB:
			rs232_Send(&huart6, resMsgBuf, idx);
		break;
		case GUNA:
			rs232_Send(&huart1, resMsgBuf, idx);
		break;
		default:
			break;
	}
	//日志打印
	#if defined (__RNL_CHADEMO_DEBUG__)
 	if(gunId == GUNA)
 	{
 		printf("GUNA ");
 	}
 	else if(gunId == GUNB)
 	{
 		printf("GUNB ");
 	}
	printf("SessionStopRes: ");
	for(i = 0; i < idx; i++)
	{
		printf("%02X ",resMsgBuf[i]);
	}
	printf("\n");
	#endif
}


